<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function insert_member($sdk_member){
		

		$str = $this->db->insert_string('sdk_member', $sdk_member); 
		$this->db->query($str);
		$mid = $this->db->insert_id();

		return  $mid > 0 ? $mid : -2;
	}
	
	function is_exist_member($siteid, $email){

		$sql = "select * from sdk_member where siteid = ?  and email = ?";
		$q = $this->db->query($sql , array($siteid, $email));
		
		return $q->num_rows() > 0 ? true : false;
	}
	
	function member_login($sdk_member){
		$sql = "select * from sdk_member where siteid= ? and email = ? and password = ?";

		return $this->db->query($sql, array($sdk_member->siteid, $sdk_member->email, $sdk_member->password)); 
	}
	
	function get_member_address($sdk_member){
		$sql = "select * from sdk_member where siteid= ? and mid = ?";
		
		return $this->db->query($sql, array($sdk_member->siteid, $sdk_member->uid));
	}
	
	function update_member_address($sdk_member){
		$data = array('shipaddr'=>$sdk_member->shipaddr, 'billaddr'=>$sdk_member->billaddr);
		$where = "mid='" . $sdk_member->uid . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);
		
		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1;
	}
	
	function update_member_name($sdk_member){
		$data = array('firstname'=>$sdk_member->firstname, 'lastname'=>$sdk_member->lastname);
		$where = "mid='" . $sdk_member->uid . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);

		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1; 
	}
	
	function update_member_password($sdk_member){
		$data = array('password'=>$sdk_member->newpassword);
		$where = "mid='" . $sdk_member->uid . "' and password='" . $sdk_member->oldpassword . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);
		
		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1;
	}
	
	function forget_password($sdk_member, $password){
		
		$data = array('password'=>$password);
		$where = "email='" . $sdk_member->email . "' and siteid='" . $sdk_member->siteid . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);
		
		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1;
	}
	
	function get_siteinfo($sdk_address){
		$sql = "select s.*,m.email from sdk_site s join sdk_member m on s.siteid = m.siteid where s.siteid='" . $sdk_address->siteid . "' and m.email='" . $sdk_address->email . "'";
		
		return $this->db->query($sql);
	}
	
	function cardholdernumrows(){
	
		$q = $this->db->query("select * from sdk_member");
	
		return $q->num_rows();
	
	}
	
	function member_list($limit,$size){
		$sql = "select * from sdk_member limit ".$limit*$size.",".$size;
		
		$q = $this->db->query($sql);
		return $q->result();
	}

	function get_member_info($par = ''){
		$sql = "select * from sdk_member where ".$par;
		$q = $this->db->query($sql);
		return $q->row();
	}
}